package dht.entity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import dht.throwable.DhtException;

public class GlobalVariables {
	public static int seed = 20130325;
	public static int replicaLevel = 3;
	public static int ringLength = 10013;
	public static String masterIp = "localhost";
	public static int masterPort = 9988;
	// public static String localIp = "localhost";
	public static int localPort = 10000;
	public static int tableUpdatePeriod = 60000;
	// public static int serverRecoverPeriod = 5000;
	// public static int replicaRecoverPeriod = 5000;
	public static int loadBalancingPeriod = 5000;
	public static int readBufferSize = 65536;
	public static int writeBufferSize = 65536;
	// public static int moveSize = 1024 * 1024 * 32;
	public static int moveNumber = 16;
	public static int moveVnodeNumber = 1;
	public static int moveConcurrentNumber = 20;
	public static int balanceThreshold = 1024 * 1024 * 32;
	public static int fileBlockSize = 65536;
	public static String dataPath = "/home/cloud/dht-fs/fs";
	public static String logFile = "/home/cloud/dht-fs/logfile.txt";
	public static String fsImage = "/home/cloud/dht-fs/data/fsimage";
	public static String slavesFile = "/home/cloud/dht-fs/slaves";

	public static int fakeFileNumber = 100000;
	public static int fakeFileMaxBlockNumber = 16;
	public static String fakeFirstName = "/home/cloud/dht-fs/data/firstName.txt";
	public static String fakeLastName = "/home/cloud/dht-fs/data/lastName.txt";
	public static String networkFile = "/home/cloud/dht-fs/data/network.dat";

	public static void initialize(String propertiesFile) throws DhtException {
		Properties properties = new Properties();
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(propertiesFile);
			properties.load(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new DhtException(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			throw new DhtException(e.getMessage());
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				e.printStackTrace();
				throw new DhtException(e.getMessage());
			}
		}

		if (properties.containsKey("seed")) {
			seed = Integer.parseInt(properties.getProperty("seed"));
		}

		if (properties.containsKey("replica-level")) {
			replicaLevel = Integer.parseInt(properties
					.getProperty("replica-level"));
		}

		if (properties.containsKey("ring-length")) {
			ringLength = Integer
					.parseInt(properties.getProperty("ring-length"));
		}

		if (properties.containsKey("master-ip")) {
			masterIp = properties.getProperty("master-ip");
		}

		if (properties.containsKey("master-port")) {
			masterPort = Integer
					.parseInt(properties.getProperty("master-port"));
		}

		// if (properties.containsKey("local-ip")) {
		// localIp = properties.getProperty("local-ip");
		// }
		//
		if (properties.containsKey("local-port")) {
			localPort = Integer.parseInt(properties.getProperty("local-port"));
		}

		if (properties.containsKey("table-update-period")) {
			tableUpdatePeriod = Integer.parseInt(properties
					.getProperty("table-update-period"));
		}

		// if (properties.containsKey("server-recover-period")) {
		// serverRecoverPeriod = Integer.parseInt(properties
		// .getProperty("server-recover-period"));
		// }
		//
		// if (properties.containsKey("replica-recover-period")) {
		// replicaRecoverPeriod = Integer.parseInt(properties
		// .getProperty("replica-recover-period"));
		// }

		if (properties.containsKey("load-balancing-period")) {
			loadBalancingPeriod = Integer.parseInt(properties
					.getProperty("load-balancing-period"));
		}

		if (properties.containsKey("read-buffer-size")) {
			readBufferSize = Integer.parseInt(properties
					.getProperty("read-buffer-size"));
		}

		if (properties.containsKey("write-buffer-size")) {
			writeBufferSize = Integer.parseInt(properties
					.getProperty("write-buffer-size"));
		}

		// if (properties.containsKey("move-size")) {
		// moveSize = Integer.parseInt(properties.getProperty("move-size"));
		// }

		if (properties.containsKey("move-number")) {
			moveNumber = Integer
					.parseInt(properties.getProperty("move-number"));
		}

		if (properties.containsKey("move-vnode-number")) {
			moveVnodeNumber = Integer.parseInt(properties
					.getProperty("move-vnode-number"));
		}

		if (properties.containsKey("move-concurrent-number")) {
			moveConcurrentNumber = Integer.parseInt(properties
					.getProperty("move-concurrent-number"));
		}

		if (properties.containsKey("balance-threshold")) {
			balanceThreshold = Integer.parseInt(properties
					.getProperty("balance-threshold"));
		}

		if (properties.containsKey("file-block-size")) {
			fileBlockSize = Integer.parseInt(properties
					.getProperty("file-block-size"));
		}

		if (properties.containsKey("fake-file-number")) {
			fakeFileNumber = Integer.parseInt(properties
					.getProperty("fake-file-number"));
		}

		if (properties.containsKey("fake-file-max-block-number")) {
			fakeFileMaxBlockNumber = Integer.parseInt(properties
					.getProperty("fake-file-max-block-number"));
		}

		if (properties.containsKey("fake-first-name")) {
			fakeFirstName = properties.getProperty("fake-first-name");
		}

		if (properties.containsKey("fake-last-name")) {
			fakeLastName = properties.getProperty("fake-last-name");
		}

		if (properties.containsKey("network-file")) {
			networkFile = properties.getProperty("network-file");
		}

		if (properties.containsKey("data-path")) {
			dataPath = properties.getProperty("data-path");
		}

		if (properties.containsKey("fs-image")) {
			fsImage = properties.getProperty("fs-image");
		}

		if (properties.containsKey("log-file")) {
			logFile = properties.getProperty("log-file");
		}
		if (properties.containsKey("slaves-file")) {
			slavesFile = properties.getProperty("slaves-file");
		}
	}

}
